package com.yuhong.springbootinit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuhong.springbootinit.mapper.CompanyPositionMapper;
import com.yuhong.springbootinit.mapper.PositionMapper;
import com.yuhong.springbootinit.model.dto.company.PositionCompanyDTO;
import com.yuhong.springbootinit.model.entity.CompanyPosition;
import com.yuhong.springbootinit.model.entity.Position;
import com.yuhong.springbootinit.service.CompanyService;
import com.yuhong.springbootinit.model.entity.Company;
import com.yuhong.springbootinit.mapper.CompanyMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author 10467
* @description 针对表【company】的数据库操作Service实现
* @createDate 2025-02-22 13:52:10
*/
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company>
    implements CompanyService {

    @Resource
    private CompanyPositionMapper companyPositionMapper;

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private PositionMapper positionMapper;

    @Override
    public List<PositionCompanyDTO> getAllPositionCompanyInfo() {
        // 获取所有职位信息
        List<Position> positions = positionMapper.selectList(null);

        List<PositionCompanyDTO> result = new ArrayList<>();

        for (Position position : positions) {
            // 使用 companyPosition 表查询对应的公司ID
            QueryWrapper<CompanyPosition> companyPositionQuery = new QueryWrapper<>();
            companyPositionQuery.eq("position_id", position.getPositionId());
            CompanyPosition companyPosition = companyPositionMapper.selectOne(companyPositionQuery);

            if (companyPosition != null) {
                String companyId = companyPosition.getCompanyId();

                // 根据 companyId 查询公司信息
                QueryWrapper<Company> companyQuery = new QueryWrapper<>();
                companyQuery.eq("company_id", companyId);
                Company company = companyMapper.selectOne(companyQuery);

                if (company != null) {
                    // 创建并添加 DTO


                    PositionCompanyDTO positionCompanyDTO = BeanUtil.copyProperties(position, PositionCompanyDTO.class);

                    positionCompanyDTO.setCompanyName(company.getCompanyName());
                    positionCompanyDTO.setCompanyId(company.getCompanyId());

                    result.add(positionCompanyDTO);
                }
            }
        }
        return result;
    }
}




